package com.sun.memo.service.impl;

import com.sun.memo.dao.BackLogDao;
import com.sun.memo.entity.BackLog;
import com.sun.memo.service.IBackLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author wys
 **/

@Service
@Transactional
public class BackLogServiceImpl implements IBackLogService {
    //添加任务
    //完成任务
    //标记任务为重要，重复调用则取消重要
    //我的一天：今天待办
    //重要日程
    //已计划日程：按照条件筛选（过期、今天（默认为）、明天、本周）
    //总任务：总览（完成以及没有完成的）
    @Autowired
    private BackLogDao backLogDao;

    @Override
    public void saveBackLog(BackLog backLog) {
        backLogDao.save(backLog);
    }

    @Override
    public void signalComplete(Integer id) {
        BackLog backLog = backLogDao.findById(id).orElse(null);
        assert backLog != null;
        backLog.setState(true);
    }

    @Override
    public void signalImportant(Integer id) {
        BackLog backLog = backLogDao.findById(id).orElse(null);
        assert backLog != null;
        backLog.setImportant(!backLog.getImportant());
    }


    @Override
    public List<BackLog> findImportantLogs(Integer id) {
        List<BackLog> all = backLogDao.findAll();
        List<BackLog> importantLogs = new ArrayList<>();
        for (BackLog backLog : all) {
            if (backLog.getImportant())
                importantLogs.add(backLog);
        }
        return importantLogs;
    }

    @Override
    public List<BackLog> findLogsByDay(String condition, Integer id) {
        switch (condition) {
            case "today":
                return findTodayLogs(id);
            case "past":
                return findPastLogs(id);
            case "tomorrow":
                return findTomorrowLogs(id);
            case "thisWeek":
                return findThisWeekLogs(id);
            default:
                return null;
        }
    }

    //1
    private List<BackLog> findTodayLogs(Integer id) {
        List<BackLog> all = findBackLogsById(id);
        List<BackLog> todayLogs = new ArrayList<>();
        Date date = new Date();
        String utilDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
        for (BackLog backLog : all) {
            String sqlDate = new SimpleDateFormat("yyyy-MM-dd").format(backLog.getDeadline());
            if (sqlDate.equals(utilDate))
                todayLogs.add(backLog);
        }
        return todayLogs;
    }

    //2
    private List<BackLog> findPastLogs(Integer id) {
        List<BackLog> all = findBackLogsById(id);
        List<BackLog> pastLogs = new ArrayList<>();
        Date date = new Date();
        String utilDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
        String[] splitUtilDate = new String[3];
        splitUtilDate = utilDate.split("-");
        int year1 = Integer.parseInt(splitUtilDate[0]);
        int month1 = Integer.parseInt(splitUtilDate[1]);
        int ri1 = Integer.parseInt(splitUtilDate[2]);
        for (BackLog backLog : all) {
            String sqlDate = new SimpleDateFormat("yyyy-MM-dd").format(backLog.getDeadline());
            String[] splitSqlDate = new String[3];
            splitSqlDate = sqlDate.split("-");
            int year2 = Integer.parseInt(splitSqlDate[0]);
            int month2 = Integer.parseInt(splitSqlDate[1]);
            int ri2 = Integer.parseInt(splitSqlDate[2]);
            if (year1 >= year2 && month1 >= month2 && ri1 > ri2)
                pastLogs.add(backLog);
        }
        return pastLogs;
    }

    //3
    private List<BackLog> findTomorrowLogs(Integer id) {
        List<BackLog> all = findBackLogsById(id);
        List<BackLog> tomorrowLogs = new ArrayList<>();
        Date date = new Date();
        String utilDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
        String[] splitUtilDate = new String[3];
        splitUtilDate = utilDate.split("-");
        Integer year1 = Integer.parseInt(splitUtilDate[0]);
        Integer month1 = Integer.parseInt(splitUtilDate[1]);
        Integer ri1 = Integer.parseInt(splitUtilDate[2]);
        String[] splitSqlDate;
        for (BackLog backLog : all) {
            String sqlDate = new SimpleDateFormat("yyyy-MM-dd").format(backLog.getDeadline());
            splitSqlDate = sqlDate.split("-");
            int year2 = Integer.parseInt(splitSqlDate[0]);
            int month2 = Integer.parseInt(splitSqlDate[1]);
            int ri2 = Integer.parseInt(splitSqlDate[2]);
            if (year1 == year2 && month1 == month2 && ri1 + 1 == ri2)
                tomorrowLogs.add(backLog);
        }
        return tomorrowLogs;
    }

    //4
    private List<BackLog> findThisWeekLogs(Integer id) {
        List<BackLog> all = findBackLogsById(id);
        List<BackLog> thisWeekLogs = new ArrayList<>();
        Date date = new Date();
        String utilDate = new SimpleDateFormat("yyyy-MM-dd").format(date);
        String[] splitUtilDate = new String[3];
        splitUtilDate = utilDate.split("-");
        int year1 = Integer.parseInt(splitUtilDate[0]);
        int month1 = Integer.parseInt(splitUtilDate[1]);
        int ri1 = Integer.parseInt(splitUtilDate[2]);
        String[] splitSqlDate;
        for (BackLog backLog : all) {
            String sqlDate = new SimpleDateFormat("yyyy-MM-dd").format(backLog.getDeadline());
            splitSqlDate = sqlDate.split("-");
            int year2 = Integer.parseInt(splitSqlDate[0]);
            int month2 = Integer.parseInt(splitSqlDate[1]);
            int ri2 = Integer.parseInt(splitSqlDate[2]);
            if (year1 == year2 && month1 == month2 && ri1 + 6 >= ri2 && ri2 >=ri1)
                thisWeekLogs.add(backLog);
        }
        return thisWeekLogs;
    }


    //找到用户名id下的log
    @Override
    public List<BackLog> findBackLogsById(Integer id) {
        List<BackLog> allBackLogs = findAllBackLogs();
        List<BackLog> backLogs = new ArrayList<>();
        for (BackLog allBackLog : allBackLogs) {
            if (allBackLog.getUser().getId().equals(id)) {
                backLogs.add(allBackLog);
            }
        }
        return backLogs;
    }

    //通过log id找到log
    @Override
    public BackLog findLogById(Integer id) {
        return backLogDao.findById(id).orElse(null);
    }

    //删除指定id的log
    @Override
    public void deleteById(Integer id) {
        backLogDao.deleteById(id);
    }

    //找到所有log，不管用户id
    @Override
    public List<BackLog> findAllBackLogs() {
        return backLogDao.findAll();
    }
}
